package cn.clx.basic.controller;

import cn.clx.basic.dto.LoginDto;
import cn.clx.basic.dto.WeChatDto;
import cn.clx.basic.exception.BusinessException;
import cn.clx.basic.service.ILoginService;
import cn.clx.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    /**
     *
     * @param dto
     * @return
     */
    @RequestMapping("/account")
    public AjaxResult account(@RequestBody LoginDto dto){
        try {
            Map<String, Object> map = loginService.account(dto);
            return AjaxResult.getAjaxResult().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("程序出错,正在殴打程序员");
        }
    }

    /**
     * 微信扫码登录
     * @param dto
     * @return
     */
    @RequestMapping("/wechat")
    public AjaxResult wechat(@RequestBody WeChatDto dto){
        try {
            return loginService.wechat(dto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("程序出错,正在殴打程序员");
        }
    }

    @RequestMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WeChatDto dto){
        try {
            return loginService.binder(dto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("程序出错,正在殴打程序员");
        }
    }

}
